<?php
 
/* setting zona waktu */
date_default_timezone_set('Asia/Jakarta');
 
$this->fpdf->FPDF("L","cm","A4");
 
// kita set marginnya dimulai dari kiri, atas, kanan. jika tidak diset, defaultnya 1 cm
$this->fpdf->SetMargins(0.5,1,0.5);
$this->fpdf->SetAutoPageBreak(false, 0.5);
 
 
/* AliasNbPages() merupakan fungsi untuk menampilkan total halaman
   di footer, nanti kita akan membuat page number dengan format : number page / total page
*/
$this->fpdf->AliasNbPages();
 
// AddPage merupakan fungsi untuk membuat halaman baru
$this->fpdf->AddPage();
 
// Setting Font : String Family, String Style, Font size 
$this->fpdf->SetFont('Arial','B',12);
 
/* Kita akan membuat header dari halaman pdf yang kita buat 
   -------------- Header Halaman dimulai dari baris ini -----------------------------
*/
$this->pdf_header();

 
/* -------------- Header Halaman selesai ------------------------------------------------*/

$month_arr = array('','Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember');
 
$this->fpdf->Ln();
$this->fpdf->SetFont('Arial','B',12);
$this->fpdf->Cell(28,1,'MONTHLY SALES REPORT',0,0,'C');
$this->fpdf->Ln();
$this->fpdf->Cell(28,1,$month_arr[$month].' '.$year,0,0,'C');
 
 
/* setting header table */
$this->fpdf->Ln(1);
$x = $this->fpdf->GetX();
$y = $this->fpdf->GetY();
$this->fpdf->SetXY($x+6, $y);
$this->fpdf->SetFont('Arial','B',10);
$this->fpdf->SetFillColor(216,216,216);
$this->fpdf->Cell(1 , 1, 'No' , 1, 'LR', 'C', true);
$this->fpdf->Cell(2 , 1, 'Date' , 1, 'LR', 'C', true);
$this->fpdf->Cell(3 , 1, 'Room' , 1, 'LR', 'C', true);
$this->fpdf->Cell(4 , 1, 'Room Type' , 1, 'LR', 'C', true);
$this->fpdf->Cell(2 , 1, 'PAX' , 1, 'LR', 'C', true);
$this->fpdf->Cell(4 , 1, 'TOTAL (Rp)' , 1, 'LR', 'C', true);
 
$no = 1;
$grand_total = 0;
$ids = array();
/* generate hasil query disini */
foreach($rooms as $row) {
	
	$this->fpdf->Ln();
	$x = $this->fpdf->GetX();
	$y = $this->fpdf->GetY();
	$this->fpdf->SetXY($x+6, $y);
	$this->fpdf->SetFont('Arial','',9);
	$this->fpdf->Cell(1 , 1, $no++ , 1, 'LR', 'C');
	$this->fpdf->Cell(2 , 1, $row['resv_room_date'] , 1, 'LR', 'C');
	$this->fpdf->Cell(3 , 1, $row['room_number'] , 1, 'LR', 'C');
	$this->fpdf->Cell(4 , 1, $row['room_type_name'] , 1, 'LR', 'C');
	$this->fpdf->Cell(2 , 1, $row['resv_adult_number']+$row['resv_child_number']+$row['resv_infant_number'] , 1, 'LR', 'C');
	
	$total = $row['price'];
	if($row['discount_percent'] != null){
		$total = $total + ($total * $row['discount_percent'] / 100);
	}
	else if($row['discount_nominal'] != null){
		$total = $total + $row['discount_nominal'];
	}
	else {
		$total = $total - 0;
	}
	$total = $total + ($total * $row['service_tax'] / 100) + ($total * $row['ppn_tax'] / 100);
	$subtotal = $total;
	
	
//	$this->fpdf->Cell(4 , 1, $this->fpdf->GetY() , 1, 'LR', 'C');
	//$grand_total = $grand_total + $total;
	if(!in_array($row['resv_id'], $ids)) {
		$ids[] = $row['resv_id'];
		
		foreach($row['services'] as $row2) {
			$total = $row2['serv_det_price'] * $row2['serv_det_qty'];
			if($row2['discount_percent'] != null){
				$total = $total - ($total * $row2['discount_percent'] / 100);
			}
			else if($row2['discount_nominal'] != null){
				$total = $total - $row2['discount_nominal'];
			}
			else {
				$total = $total - 0;
			}		
			$total = $total + ($total * $row2['service_tax'] / 100) + ($total * $row2['ppn_tax'] / 100);
			$subtotal = $subtotal + $total;
		}
	}
	
	foreach($row['materials'] as $row2) {
		$total = $row2['resv_mat_price'] * $row2['resv_mat_qty'];
		if($row['discount_percent'] != null){
			$total = $total - ($total * $row['discount_percent'] / 100);
		}
		else if($row['discount_nominal'] != null){
			$total = $total - $row['discount_nominal'];
		}
		else {
			$total = $total - 0;
		}
		$total = $total + ($total * $row['service_tax'] / 100) + ($total * $row['ppn_tax'] / 100);
		$subtotal = $subtotal + $total;
	}
	
	$this->fpdf->Cell(4 , 1, 'Rp. '.number_format($subtotal,2,',','.') , 1, 'LR', 'R');
	
	$grand_total = $grand_total + $subtotal;
	if($this->fpdf->GetY() >= 17.5) {
		$this->pdf_footer();
		$this->fpdf->AddPage('L', 'A4');
		$this->pdf_header();
	}
	
}

$this->fpdf->Ln();
$x = $this->fpdf->GetX();
$y = $this->fpdf->GetY();
$this->fpdf->SetXY($x+6, $y);
$this->fpdf->SetFont('Arial','B',10);
$this->fpdf->Cell(12 , 1, 'TOTAL' , 1, 'LR', 'C', true);
$this->fpdf->Cell(4 , 1, 'Rp. '.number_format($grand_total,2,',','.') , 1, 'LR', 'R', true);

$this->pdf_footer();

/* generate pdf jika semua konstruktor, data yang akan ditampilkan, dll sudah selesai */
$this->fpdf->Output("monthly_sales.pdf","I");
?>